package com.d.ui.view.verinor.adapter;

import com.d.ui.view.ResourceTable;
import com.d.ui.view.verinor.models.VerModel;
import java.util.ArrayList;
import java.util.List;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

public class VerAdapter extends BaseItemProvider {

  private ArrayList<Component> childComponents = new ArrayList<>();
  private List<VerModel> dataSet;
  private Context mContext;

  public VerAdapter(Context mContext, List<VerModel> dataSet) {
    this.mContext = mContext;
    this.dataSet = dataSet;
    for (int i = 0; i < dataSet.size(); i++) {
      childComponents.add(null);
    }
  }

  @Override
  public int getCount() {
    return this.dataSet != null ? this.dataSet.size() : 0;
  }

  @Override
  public Object getItem(int i) {
    if (dataSet != null && i >= 0 && i < dataSet.size()) {
      return dataSet.get(i);
    }
    return null;
  }

  @Override
  public long getItemId(int i) {
    return i;
  }

  @Override
  public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
    Component childComponent = childComponents.get(i);
    VerModel verModel = dataSet.get(i);
    if (verModel.type == 0) {
      childComponent = getVerTextComponent(childComponent, verModel);
    } else {
      childComponent = getVerInorComponent(childComponent, verModel);
    }

    childComponents.set(i, childComponent);
    return childComponent;
  }

  private Component getVerTextComponent(Component component, VerModel verModel) {
    if (component == null) {
      component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_adapter_ver_text
          , null, true);
    }
    Text childText = (Text) component.findComponentById(ResourceTable.Id_tv_desc);
    childText.setText(verModel.content);
    component.setClickedListener(c -> {
      new ToastDialog(mContext).setText("Click at: " + verModel.content).setAlignment(LayoutAlignment.CENTER).show();
    });
    return component;
  }

  private Component getVerInorComponent(Component component, VerModel verModel) {
    if (component != null) {
      return component;
    }
    component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_adapter_ver_inor,
        null, true);
    ListContainer listContainer =
        (ListContainer) component.findComponentById(ResourceTable.Id_rv_ver_inor);
    VerInorAdapter adapter = new VerInorAdapter(mContext, verModel.dataSet);
    listContainer.setItemProvider(adapter);
    listContainer.setScrolledListener(new Component.ScrolledListener() {
      @Override
      public void onContentScrolled(Component component, int i, int i1, int i2, int i3) {
        verModel.offSetX = i;
      }

    });
    listContainer.setBindStateChangedListener(new Component.BindStateChangedListener() {
      @Override
      public void onComponentBoundToWindow(Component component) {
        listContainer.scrollTo(verModel.offSetX, 0);
      }

      @Override
      public void onComponentUnboundFromWindow(Component component) {

      }
    });
    // listContainer.scrollTo(verModel.offSetX, 0);
    // listContainer.scrollTo(2);
    return component;
  }
}
